package com.yingxin.yxerp.module.print.demo;


import cn.hutool.core.date.DateUtil;
import com.yingxin.yxerp.module.common.util.JasperReportUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


// @Api(tags = "打印demo")
@RestController
@RequestMapping("/demo/print")
public class PrintDemoController {


    @GetMapping("/jasper")
    public void getReport(@RequestParam("type") String reportType, HttpServletResponse response)throws Exception {
        HashMap<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("name1", "PT001");
        parameters.put("name2", "CGHT999888777");
        parameters.put("name3", DateUtil.format(new Date(),"yyyy-MM-dd HH:mm"));
        parameters.put("name4", "山东新泰新材料有限公司");
        parameters.put("name5", "陈年");
        parameters.put("name6", "j");
        parameters.put("name7", "宝哥");
        parameters.put("name8", "陈传龙");
        parameters.put("name9", "yang");
        parameters.put("name10", "998998kg");
        List<HashMap> list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            HashMap<String, String> item = new HashMap<String, String>();
            item.put("Field_1",  "Field_1-" + i);
            item.put("Field_2",  "Field_2-" + i);
            item.put("Field_3",  "Field_3-" + i);
            item.put("Field_4",  "Field_4-" + i);
            item.put("Field_5",  "Field_5-" + i);
            item.put("Field_6",  "Field_6-" + i);
            item.put("Field_7",  "Field_7-" + i);
            list.add(item);
        }
        String jasperPath = JasperReportUtil.getJasperFileDir("rzInStock");
        if (reportType.equals("pdf")) {
            JasperReportUtil.exportToPdf(jasperPath, parameters, list, response);
        } else if (reportType.equals("html")) {
            JasperReportUtil.exportToHtml(jasperPath, parameters, list, response);
        }
    }

}
